package org.calzz.jajb.converter;

import org.calzz.jajb.JsonDeserializer;
import org.calzz.jajb.JsonSerializer;
import org.calzz.jajb.io.IWriter;
import org.calzz.jajb.io.JsonWriter;
import org.springframework.core.MethodParameter;

/**
 * Main interface for all converters than can be used in the
 * Serializer/Deserializer. Implement this interface if You want to change the
 * default converter for a specific type. Make sure Your <code>canConvert</code>
 * method returns <code>true</code> for, and only for types You want to
 * convert.
 * 
 * @author Marek Brodziak <marek.brodziak@gmail.com>
 * @author Piotr Pejas <piotr.pejas@gmail.com>
 * @since Feb 6, 2008
 */
public interface IConverter {

	/**
	 * Serialize Object to JSON string, do not call directly
	 * @see org.calzz.jajb.io.JsonWriter
	 * @param obj Object to convert
	 * @param name Object name
	 * @param writer {@link JsonWriter} instance
	 * @param serializer {@link JsonSerializer} instance
	 */
	void marshal(Object obj, String name, IWriter writer, JsonSerializer serializer);

	/**
	 * Deserialize Object from a JSON string, do not call directly.
	 * @param env environment of the deserialized object
	 * @param clazz class of the object
	 * @param deserializer {@link JsonDeserializer} instance
	 * @param methodParam Spring class combining <code>Method</code> object with information
	 * 			about which parameter is being unmarshaled by this method
	 * @return hopefully deserialized object of a given class
	 */
	<T> Object unmarshal(Object env, Class<T> clazz, JsonDeserializer deserializer, MethodParameter methodParam);

	/**
	 * Check if class can be converted by this converter. It is used by the
	 * Serializer and deserializer for finding converters for specific types.
	 * @param clazz class which you want convert
	 * @return true if can be converted
	 */
	boolean canConvert(Class<?> clazz);
}
